﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.Student;
using LJX.SmartPark.Service.Repository;
using LJX.SmartPark.Service.StudentServices.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LJX.SmartPark.Service.StudentServices
{
    public class StudentService : IStudentService
    {
        private readonly IRepository<Student> _studentService;

        public StudentService(IRepository<Student> studentService)
        {
            _studentService = studentService;
        }

        public Response<StudentInput> GetStudentById(StudentInput input)
        {
            //var student2 =  _studentService.GetListQuery().WhereIf(!string.IsNullOrWhiteSpace(input.Name), m=>m.Id==input.Id);

            var student = _studentService.GetListQuery().FirstOrDefault(m => m.Id == input.Id);
            var result = student.MapTo<StudentInput>();
            return Response<StudentInput>.Ok(result);
        }

        public async Task<Response<List<StudentInput>>> GetStudentListAsync(StudentInput input)
        {
            var students = await _studentService.GetAllListAsync();
            var result = students.MapToList<StudentInput>();
            return Response<List<StudentInput>>.Ok(result);
        }

        public async Task<Response<bool>> PostAddStudent(StudentInput input)
        {
            var result = _studentService.InsertAsyncById(input.MapTo<Student>());

            return Response<bool>.Ok(result.Result > 0);
        }

        public async Task<Response<bool>> PutUpdateStudent(StudentInput input)
        {
            var result = _studentService.UpdateAsync(input.MapTo<Student>());

            return Response<bool>.Ok(result.Result > 0);
        }

        public async Task<Response<bool>> DeleteByIdAsync(StudentInput input)
        {
            var result = await _studentService.DeleteByIdAsync(input.MapTo<Student>());
            return Response<bool>.Ok(result > 0);
        }
    }
}
